//
//  GHMineHeaderView.m
//  GodHorses
//
//  Created by Mac on 2017/11/15.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHMineHeaderView.h"
#import "NSString+STFormatter.h"
#import "GHOrderModel.h"
@interface GHMineHeaderView()
@property(nonatomic, strong) STButton                     *iconButton;
@property(nonatomic, strong) STButton                     *vipButton;
@property(nonatomic, strong) STLabel                     *nickNameLable;

@property(nonatomic, strong) GHMenuControl                *yueControl;
@property(nonatomic, strong) GHMenuControl                *yejiControl;
@property(nonatomic, strong) GHMenuControl                *jifenControl;
@property(nonatomic, strong) GHMenuControl                *shenmaControl;
@property(nonatomic, strong) GHMenuControl                *currentShenmaControl;
@property(nonatomic, strong) GHMenuControl                *leijiShenmaControl;


@property(nonatomic, strong) STLabel                     *willPaybadgeValueLable;
@property(nonatomic, strong) STLabel                     *willFahuobadgeValueLable;
@property(nonatomic, strong) STLabel                     *willShouhuobadgeValueLable;
@property(nonatomic, strong) STLabel                     *willCommentbadgeValueLable;
@property(nonatomic, strong) STLabel                     *didFinshbadgeValueLable;
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation GHMineHeaderView
- (void)setUserModel:(GHUserModel *)userModel{
    _userModel = userModel;
    [self.iconButton sd_setBackgroundImageWithURL:[NSURL URLWithString:_userModel.face] forState:UIControlStateNormal];
    self.nickNameLable.text = _userModel.username;
    CGFloat titleWith = [self.nickNameLable.text st_widthWithheight:15 font:14];
    titleWith = titleWith < 200?titleWith:200;
    self.nickNameLable.st_width = titleWith;
    self.nickNameLable.st_centerX = UIScreenWidth / 2 - 30;
    self.vipButton.st_left = self.nickNameLable.st_right + 5;
    [self.vipButton setTitle:[NSString stringWithFormat:@"vip%ld",[_userModel.user_type integerValue] + 1] forState:UIControlStateNormal];
    
    //金钱￥
    self.yueControl.valueLable.text = [NSString stringWithFormat:@"￥ %@",self.userModel.u_money];
    self.yejiControl.valueLable.text = [NSString stringWithFormat:@"￥ %@",self.userModel.u_distribution];
     self.jifenControl.valueLable.text = [NSString stringWithFormat:@"￥ %@",self.userModel.pay_points];
    self.shenmaControl.valueLable.text = [NSString stringWithFormat:@"￥ %@",self.userModel.wfpoints];
    self.currentShenmaControl.valueLable.text = [NSString stringWithFormat:@"￥ %@",self.userModel.now_wfpoints];
    self.leijiShenmaControl.valueLable.text = [NSString stringWithFormat:@"￥ %@",self.userModel.all_wfpoints];
    
     [self updateBadgeValueRequest];
}
#pragma mark --subView
- (void)configSubView{
    self.backgroundColor = GH_backgroundColor;
    [self configTopView];
    CGFloat bootom =  [self configMoneyView];
    [self configOrderView:bootom];
}
- (void)configTopView{
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 200)];
    bannerImageView.image = [UIImage imageNamed:@"img_mine_banner"];
    bannerImageView.userInteractionEnabled = YES;
    [self addSubview:bannerImageView];
    __weak typeof(self) weakSelf =  self;
    self.iconButton = [[STButton alloc] initWithFrame:CGRectMake(0, 50, 80, 80)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:40
                                           backgroundColor:nil
                                           backgroundImage:[UIImage imageNamed:@"1"]
                                                     image:nil];
    self.iconButton.st_centerX = UIScreenWidth / 2;
    [self.iconButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedIconButton];
    }];
    [bannerImageView addSubview:self.iconButton];
    
    STButton * settingButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"setting"]];
    settingButton.st_top  = 5;
    settingButton.st_right = UIScreenWidth ;
    [settingButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedSettingButton];
    }];
    [bannerImageView addSubview:settingButton];
    
    STLabel * nameLable = [[STLabel alloc] initWithFrame:CGRectMake(0, self.iconButton.st_bottom + 20, 70, 15)
                                                     text:@"----"
                                                textColor:[UIColor whiteColor]
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentRight];
    self.nickNameLable = nameLable;
    self.nickNameLable.st_centerX = UIScreenWidth / 2 - 30;;
    [bannerImageView addSubview:nameLable];
    
    STButton * vipButton = [[STButton alloc] initWithFrame:CGRectMake(self.nickNameLable.st_right + 5, 0, 55, 22)
                                                     title:@"vip1"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:11
                                              cornerRadius:11
                                           backgroundColor:GH_YellowBackGroundColor
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"VIP"]];
    self.vipButton = vipButton;
    self.vipButton.st_centerY = self.nickNameLable.st_centerY;
    self.vipButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.vipButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [bannerImageView addSubview:self.vipButton];
    
}
- (CGFloat)configMoneyView{
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 200 + 10, UIScreenWidth, 70)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteView];
    //可提现
    self.yueControl = [[GHMenuControl alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 3, 70)
                                                     value:@"￥ 0.00"
                                                      name:@"余额"
                                                  showLine:YES];
    [whiteView addSubview:self.yueControl];
    
    self.yejiControl = [[GHMenuControl alloc] initWithFrame:CGRectMake(self.yueControl.st_right, 0, UIScreenWidth / 3, 70)
                                                     value:@"￥ 0.00"
                                                      name:@"业绩"
                                                  showLine:YES];
    [whiteView addSubview:self.yejiControl];
    
    self.jifenControl = [[GHMenuControl alloc] initWithFrame:CGRectMake(self.yejiControl.st_right, 0, UIScreenWidth / 3, 70)
                                                      value:@"￥ 0.00"
                                                       name:@"购物券积分"
                                                   showLine:NO];
   [whiteView addSubview:self.jifenControl];
    
    //直线
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, self.yueControl.st_bottom, UIScreenWidth, 0.5)];
    line.backgroundColor = GH_lineColor;
    [whiteView addSubview:line];
    
    self.shenmaControl = [[GHMenuControl alloc] initWithFrame:CGRectMake(0, self.yueControl.st_bottom, UIScreenWidth / 3, 70)
                                                      value:@"0个"
                                                       name:@"神马值"
                                                   showLine:YES];
    [whiteView addSubview:self.shenmaControl];
    
    self.currentShenmaControl = [[GHMenuControl alloc] initWithFrame:CGRectMake(self.shenmaControl.st_right, self.yueControl.st_bottom, UIScreenWidth / 3, 70)
                                                        value:@"￥ 0.00"
                                                         name:@"当前神马值"
                                                     showLine:YES];
    [whiteView addSubview:self.currentShenmaControl];
    
    self.leijiShenmaControl = [[GHMenuControl alloc] initWithFrame:CGRectMake(self.currentShenmaControl.st_right, self.yueControl.st_bottom, UIScreenWidth / 3, 70)
                                                               value:@"￥ 0.00"
                                                                name:@"累计神马值"
                                                            showLine:YES];
    [whiteView addSubview:self.leijiShenmaControl];
    whiteView.st_height = self.leijiShenmaControl.st_bottom;
    
    
    __weak typeof(self) weakSelf =  self;
    //充值提现
    UIView * moneyView = [[UIView alloc] initWithFrame:CGRectMake(0, whiteView.st_bottom + 10, UIScreenWidth, 70)];
    moneyView.backgroundColor = [UIColor whiteColor];
    [self addSubview:moneyView];
    
    STButton * chongzhiButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)
                                                     title:@"充值"
                                                titleColor:GH_YellowBackGroundColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"PC_chongzhi"]];
    chongzhiButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    chongzhiButton.st_centerX = UIScreenWidth / 4;
    chongzhiButton.st_centerY = 35;
    [chongzhiButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedChongzhiButton:sender];
    }];
    [moneyView addSubview:chongzhiButton];
    
    STButton * tixianButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)
                                                          title:@"提现"
                                                     titleColor:GH_YellowBackGroundColor
                                                      titleFont:15
                                                   cornerRadius:0
                                                backgroundColor:nil
                                                backgroundImage:nil
                                                          image:[UIImage imageNamed:@"PC_tixian"]];
    tixianButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    tixianButton.st_centerX = 3 * UIScreenWidth / 4;
    tixianButton.st_centerY = 35;
    [tixianButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedTiXianButton:sender];
    }];
    [moneyView addSubview:tixianButton];
    
    //线
    UIView * line3 = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 1, 45)];
    line3.backgroundColor = GH_lineColor;
    line3.st_centerX =  UIScreenWidth / 2;
    line3.st_centerY = 35;
    [moneyView addSubview:line3];
    
    return moneyView.st_bottom;
}
- (void)configOrderView:(CGFloat)top{
    UIView * orderView = [[UIView alloc] initWithFrame:CGRectMake(0, top + 10, UIScreenWidth, 35)];
    orderView.backgroundColor = UIColorFromRGBA(0xfcfcf7);
    [self addSubview:orderView];
    
    STButton * orderButton = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 200, 16)
                                                     title:@"商城订单"
                                                titleColor:GH_firstTextColor
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"PC_shangchengdingdan"]];
    orderButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    orderButton.st_centerY = orderView.st_height / 2;
    [orderView addSubview:orderButton];
    //订单按钮
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, orderView.st_bottom, UIScreenWidth, 70)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:whiteView];

    UIControl * daifukuan = [self controlWithImageName:@"PC_daifukuan" title:@"待付款" tag:10001];
    daifukuan.st_left = 0;
    [whiteView addSubview:daifukuan];
    UIControl * daifahuo = [self controlWithImageName:@"PC_daifahuo" title:@"待发货" tag:10002];
    daifahuo.st_left = 0 + UIScreenWidth / 5;
    [whiteView addSubview:daifahuo];
    UIControl * daishouhuo = [self controlWithImageName:@"PC_daishouhuo" title:@"待收货" tag:10003];
    daishouhuo.st_left = 0 + 2 * UIScreenWidth / 5;
    [whiteView addSubview:daishouhuo];
    UIControl * daipingjia = [self controlWithImageName:@"PC_daipingjia" title:@"待评价" tag:10004];
    daipingjia.st_left = 0 + 3 * UIScreenWidth / 5;
    [whiteView addSubview:daipingjia];
    UIControl * shouhouzhong = [self controlWithImageName:@"PC_shouhouzhong" title:@"已完成" tag:10005];
    shouhouzhong.st_left = 0 + 4 * UIScreenWidth / 5;
    [whiteView addSubview:shouhouzhong];
    
    self.st_height = whiteView.st_bottom + 10;
    
   
}
- (UIControl*)controlWithImageName:(NSString*)imageName title:(NSString*)title tag:(NSInteger)tag{
    UIControl * control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth / 5, 80)];
    control.backgroundColor = [UIColor whiteColor];
    control.tag = tag;
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 22, 24, 24 )];
    imageView.image = [UIImage imageNamed:imageName];
    imageView.st_centerX = control.st_width / 2;
    [control addSubview:imageView];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, imageView.st_bottom + 10, control.st_width, 13)
                                                     text:title
                                                textColor:GH_firstTextColor
                                                     font:12
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [control addSubview:titleLable];
    [control addTarget:self action:@selector(onSelectedOrderControl:) forControlEvents:UIControlEventTouchUpInside];
    STLabel * badgeValeLable = [[STLabel alloc] initWithFrame:CGRectMake(imageView.right - 6, 0, 20, 14)
                                                     text:@""
                                                textColor:[UIColor whiteColor]
                                                     font:10
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    badgeValeLable.bottom = imageView.top + 6;
    badgeValeLable.layer.cornerRadius = 6;
    badgeValeLable.clipsToBounds = YES;
    badgeValeLable.backgroundColor = GH_redColor;
    badgeValeLable.hidden = YES;
    [control addSubview:badgeValeLable];
    
    if (tag == 10001) {
        self.willPaybadgeValueLable = badgeValeLable;
    }
    if (tag == 10002) {
        self.willFahuobadgeValueLable = badgeValeLable;
    }
    if (tag == 10003) {
        self.willShouhuobadgeValueLable = badgeValeLable;
    }
    if (tag == 10004) {
        self.willCommentbadgeValueLable = badgeValeLable;
    }
    if (tag == 10005) {
        self.didFinshbadgeValueLable = badgeValeLable;
    }
    return control;
}
#pragma mark --Action Method
- (void)onSelectedIconButton{
    if (self.delegate && [self.delegate  respondsToSelector:@selector(onSelectedIconButton)]) {
        [self.delegate onSelectedIconButton];
    }
}
- (void)onSelectedSettingButton{
    if (self.delegate && [self.delegate  respondsToSelector:@selector(onSelectedSettingButton)]) {
        [self.delegate onSelectedSettingButton];
    }
}
- (void)onSelectedChongzhiButton:(UIButton*)sender{
    if (self.delegate && [self.delegate  respondsToSelector:@selector(onSelectedChongzhiButton)]) {
        [self.delegate onSelectedChongzhiButton];
    }
}
- (void)onSelectedTiXianButton:(UIButton*)sender{
    if (self.delegate && [self.delegate  respondsToSelector:@selector(onSelectedTiXianButton)]) {
        [self.delegate onSelectedTiXianButton];
    }
}
- (void)onSelectedOrderControl:(UIControl*)control{
    NSString * title;
    if (control.tag == 10001) {
        title = @"待付款";
    }
    if (control.tag == 10002) {
        title = @"待发货";
    }
    if (control.tag == 10003) {
        title = @"待收货";
    }
    if (control.tag == 10004) {
        title = @"待评价";
    }
    if (control.tag == 10005) {
        title = @"已完成";
    }
    if (self.delegate && [self.delegate  respondsToSelector:@selector(onSlectedOrderButton:)]) {
        [self.delegate onSlectedOrderButton:title];
    }
}
//偷个懒
- (void)updateBadgeValueRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@(1) forKey:@"page"];
    //-1获取全部订单0待支付订单1待发货订单2待收货订单3待评价订单4已完成订单
    NSString * type = @"-1";
    [dic setObject:type forKey:@"order_type"];
    
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/order/get_my_orders"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {

                                                 
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 //获取订单数组
                                                 NSInteger daifukuan = 0;
                                                  NSInteger daifahuo = 0;
                                                  NSInteger daishouhuo = 0;
                                                  NSInteger daipingjia = 0;
                                                  NSInteger yiwancheng = 0;
                                                 if ([dataDic.allKeys containsObject:@"order_info"]) {
                                                     NSArray * orderDics = dataDic[@"order_info"];
                                                     for (NSDictionary * orderDic in orderDics) {
                                                         GHOrderModel *  model = [GHOrderModel mj_objectWithKeyValues:orderDic];
                                                         if ([dataDic.allKeys containsObject:@"order_type"]) {
                                                             //订单类型
                                                             NSInteger order_type = [dataDic[@"order_type"] integerValue];
                                                             model.order_type = order_type;
                                                             //订单状态 0待支付订单1待发货订单2待收货订单3待评价订单4已完成订单 5 已取消
                                                             if (model.order_status == 0) {
                                                                 daifukuan ++;
                                                             }
                                                             if (model.order_status== 1) {
                                                                 daifahuo ++;
                                                             }
                                                             if (model.order_status == 2) {
                                                                 daishouhuo ++;
                                                             }
                                                             if (model.order_status == 3) {
                                                                 daipingjia ++;
                                                             }
                                                             if (model.order_status == 4) {
                                                                 yiwancheng ++;
                                                             }
                                                         }
                                                         [self.dataSouce addObject:model];
                                                     }
                                                     
                                                 }
                                                 self.willPaybadgeValueLable.hidden = !daifukuan;
                                                 self.willPaybadgeValueLable.text = @(daifukuan).description;
                                                 
                                                 self.willFahuobadgeValueLable.hidden = !daifahuo;
                                                 self.willFahuobadgeValueLable.text = @(daifahuo).description;
                                                 
                                                 self.willShouhuobadgeValueLable.hidden = !daishouhuo;
                                                 self.willShouhuobadgeValueLable.text = @(daishouhuo).description;
                                                 
                                                 self.willCommentbadgeValueLable.hidden = !daipingjia;
                                                 self.willCommentbadgeValueLable.text = @(daipingjia).description;
                                                 
                                                 self.didFinshbadgeValueLable.hidden = !yiwancheng;
                                                 self.didFinshbadgeValueLable.text = @(yiwancheng).description;
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                             }];
}

@end
